//
//  TLTimerViewController.m
//  ReactiveCocoa_Practice
//
//  Created by thinker on 15/8/27.
//  Copyright (c) 2015年 RayTao. All rights reserved.
//

#import "TLTimerViewController.h"

#import "TLTimerViewModel.h"
#import <ReactiveCocoa/ReactiveCocoa.h>


@interface TLTimerViewController ()

@property (weak, nonatomic) IBOutlet UILabel *timeRemainingLabel;
@property (weak, nonatomic) IBOutlet UILabel *stepNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *nextStepLabel;
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
@property (weak, nonatomic) IBOutlet UIButton *pauseBtn;

@end

@implementation TLTimerViewController

#pragma mark - life cycle
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.viewModel = [[TLTimerViewModel alloc] init];

    self.title = [self.viewModel recipeName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
    
    // Configure subviews
    self.timeRemainingLabel.layer.cornerRadius = CGRectGetHeight(self.timeRemainingLabel.frame) / 2.0f;
    self.timeRemainingLabel.layer.borderWidth = 5.0f;
    self.timeRemainingLabel.layer.borderColor = [[self colorWithHexString:@"DE9726"] CGColor];
    self.timeRemainingLabel.textColor = [self colorWithHexString:@"522404"];
    
    [self.startBtn addTarget:self action:@selector(resume) forControlEvents:UIControlEventTouchUpInside];
    [self.pauseBtn addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
    
    // Reactive Bindings
    RAC(self.timeRemainingLabel, text) = RACObserve(self.viewModel, timeRemainingString);
    
}

- (void)viewWillAppear:(BOOL)animated {
    [self.viewModel setActive:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    
}

- (void)viewWillDisappear:(BOOL)animate {
    
    
}

- (void)viewDidLayoutSubviews {
}

#pragma mark - UITableViewDelegate



#pragma mark - CustomDelegate



#pragma mark - event response



#pragma mark - private methods

- (void)setBarButtonItem {
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain  target:self  action:nil];
    self.navigationItem.backBarButtonItem = backButton;
}

- (void)setNavigationBar {
    UINavigationBar *navBar = self.navigationController.navigationBar;
    navBar.tintColor = [UIColor whiteColor];
    navBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
}

- (void)setUIContent {
//    [self setNavigationBar];
//    [self setBarButtonItem];
}

- (void)setViewsMASConstraint {
    
}

#pragma mark - getters and settters



#pragma mark - debug operation
- (void)updateOnClassInjection {
    
}




// Returns a UIColor by scanning the string for a hex number and passing that to +[UIColor colorWithRGBHex:]
// Skips any leading whitespace and ignores any trailing characters
- (UIColor *)colorWithHexString:(NSString *)stringToConvert {
    
    if ([stringToConvert hasPrefix:@"#"]) {
        stringToConvert = [stringToConvert substringFromIndex:1];
    }
    
    NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
    unsigned hexNum;
    
    if (![scanner scanHexInt:&hexNum]) {
        return nil;
    }
    
    return [self colorWithRGBHex:hexNum];
}

- (UIColor *)colorWithRGBHex:(UInt32)hex {
    
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex) & 0xFF;
    
    return [UIColor colorWithRed:r / 255.0f
                           green:g / 255.0f
                            blue:b / 255.0f
                           alpha:1.0f];
}


-(void)pause {
    [self.viewModel pause];

}

-(void)resume {
    [self.viewModel resume];
}

-(void)cancel {
    [self dismiss];
}

-(void)dismiss {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
